#include "RingQueue.hpp"
#include <cstdlib>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>



void* consumer(void* args)
{
    RingQueue<int>* rq = (RingQueue<int>*)args;

    while (true)
    {
        int x;
        //1.从环形队列中获取数据或者任务
        rq->pop(&x);
        //2.进行一定的处理 -- 也会有时间消耗
        std::cout << "消费: " << x << " [" << pthread_self() << "]" << std::endl;
        sleep(1);

    }
}

void* productor(void* args)
{
    RingQueue<int>* rq = (RingQueue<int>*)args;

    while (true)
    {
        //1.构建任务对象或者数据 -- 一般是可以从外部来 -- 也会有时间消耗
        int x = rand() % 100 + 1;

        //2.推送到环形队列
        rq->push(x);//完成生产的过程
        std::cout << "生产: " << x << " [" << pthread_self() << "]" << std::endl;
        sleep(1);
    }
}

int main()
{
    srand((uint64_t)time(nullptr) ^ getpid() ^ 0x1235135);

    RingQueue<int>* rq = new RingQueue<int>();
    // rq->debug();

    pthread_t c[3], p[2];
    pthread_create(c, nullptr, consumer, (void*)rq);
    pthread_create(c + 1, nullptr, consumer, (void*)rq);
    pthread_create(c + 2, nullptr, consumer, (void*)rq);
    pthread_create(p, nullptr, productor, (void*)rq);
    pthread_create(p + 1, nullptr, productor, (void*)rq);

    for (int i = 0; i < 3; i++)
    {
        pthread_join(c[i], nullptr);
    }

    for (int i = 0; i < 2; i++)
    {
        pthread_join(p[i], nullptr);
    }

    return 0;
}